package com.st.service.impl;

import com.api.tickets.ITicketsClient;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.entity.Result;
import com.st.entity.ScenicDetail;
import com.st.entity.Ticket;
import com.st.mapper.IScenicMapper;
import com.st.service.IScenicService;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

@Service
public class IScenicServiceImpl extends ServiceImpl<IScenicMapper, ScenicDetail> implements IScenicService {
    @Autowired
    private IScenicMapper iScenicMapper;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private ITicketsClient iTicketsClient;

    @Value("${topic.es}")
    private String esTopicName;
    @Value("${topic.page}")
    private String pageTopicName;
    @Override
    public ScenicDetail findScenicDetailByAid( Integer aid) {
        ScenicDetail scenicDetail = iScenicMapper.selectById(aid);
        Ticket ticketByAid = iTicketsClient.getTicketBy(aid);
        scenicDetail.setTicket(ticketByAid);
        return scenicDetail;
    }
    @Override
    public Result audit(Integer attractionsId) {
        //获取spu信息
        ScenicDetail scenicDetail = iScenicMapper.selectById(attractionsId);
        Ticket ticketByAid = iTicketsClient.getTicketBy(attractionsId);
        scenicDetail.setTicket(ticketByAid);
        //WxbGoods wxbGoods = wxbGoodsMapper.selectById(spuId);
        if (scenicDetail != null) {
            //修改状态
            //wxbGoods.setAuditStatus("1");
            //int i = wxbGoodsMapper.updateById(wxbGoods);
                //获取spu的详细信息
                //WxbGoods wxbGoodsInfo = wxbGoodsMapper.findGoodsSpuInfoById(spuId);
                //同步索引库
                //searchClient.save(wxbGoodsInfo);
                Integer spuId = scenicDetail.getAttractionsId();
                //发送消息[同步索引库]
                SendResult sendResult = rocketMQTemplate.syncSend(esTopicName + ":" + "es-tag", scenicDetail);
                //发送消息[生成静态化页面]
                rocketMQTemplate.syncSend(pageTopicName + ":" + "page-tag",spuId);
                return  new Result(true,"审核成功");
        }else{
            return  new Result(false,"审核失败");
        }
    }

    @Override
    public ScenicDetail findScenicById(Integer id) {
        return iScenicMapper.selectById(id);
    }
}
